/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under Ultimate Liberty license
  * SLA0044, the "License"; You may not use this file except in compliance with
  * the License. You may obtain a copy of the License at:
  *                             www.st.com/SLA0044
  *
  ******************************************************************************
  */
/* Includes ------------------------------------------------------------------*/
#include "fos.h"
#include "osc_menu.h"
#include "string.h"
#include "osc_cfg.h"
#include "osc_param.h"
#include "k24c02.h"
#include "osc.h"
#include "hal_iic.h"
/* Private includes ----------------------------------------------------------*/
/* Includes ------------------------------------------------------------------*/
FOS_TSK_REGISTER(osc_param_thread,PRIORITY_IDLE,100); /* gui detecter task run as idle */
FOS_INODE_REGISTER("osc_thread",osc_param_heap,osc_param_init,0,8);
/* FOS task */
/* iic sta change */
FOS_TSK_REGISTER(hal_iic_sta,PRIORITY_4,100);
/* tmp param */
static osc_run_msg_def osc_m_tmp;
/* dfe */
static osc_run_msg_def * runmsg;
/* sta */
static unsigned int iic_sta = 0;
/* heap init */
static int osc_param_heap(void)
{
	return FS_OK;
}
/* check sum */
static unsigned char osc_param_check(unsigned char * dat,unsigned int len)
{
	/* sum */
	unsigned char xor_t = 0;
	/* ce */
	for( int i = 0 ; i < len ; i ++ )
	{
		xor_t += dat[i];
	}
	/* return */
	return xor_t;
}
/* osc_param_init */
static int osc_param_init(void)
{
	/* get osc */
	runmsg = get_run_msg();
	/* read data */
	osc_k24C02_read(90+2,&osc_m_tmp,sizeof(osc_m_tmp));
	/* check */
	if( osc_m_tmp.check == osc_param_check((unsigned char *)&osc_m_tmp,sizeof(osc_m_tmp)- 1))
	{
		/* check ok */
		memcpy(runmsg,&osc_m_tmp,sizeof(osc_m_tmp));
	  /* ok */
	}
	else
	{
		/* get draw area */
	  const draw_area_def * area = get_draw_area_msg();	
		/* init */
		memset(runmsg,0,sizeof(osc_run_msg_def));		
		/* set default vol offset */
		runmsg->vol_offset_scale[0] = area->total_pixel_v / 2;//middle
		runmsg->vol_offset_scale[1] = area->total_pixel_v / 2;//middle
		/* set vol scale */
		runmsg->vol_scale_ch[0] = 8;//2V div
		runmsg->vol_scale_ch[1] = 8;//2V div
		/* set trig scale */
		runmsg->trig_vol_level_ch[0] = area->total_pixel_v / 2;//middle
		runmsg->trig_vol_level_ch[1] = area->total_pixel_v / 2;//middle
		/* time */
		runmsg->time_scale = 5;
		/* set default measure item */
		runmsg->measure_item[0] = (1<<0)|(1<<2)|(1<<6);
		/* set id */
		runmsg->check = osc_param_check((unsigned char *)runmsg,sizeof(osc_m_tmp)- 1);	
		/* copy data */
		memcpy(&osc_m_tmp,runmsg,sizeof(osc_m_tmp));		
		/* save data */
		osc_k24c02_write(90+2,&osc_m_tmp,sizeof(osc_m_tmp));
		/* end */		
	}
	/* return */
	return FS_OK;
}
/* save sata to eeprom */
int osc_param_save(void)
{
	/* disable all it */
	osc_disable_all_it();
	/* check */
	osc_run_msg_def tdp;	
	/* read */
	osc_k24C02_read(90+2,&tdp,sizeof(tdp));	
	/* check */
	if( memcmp(runmsg,&osc_m_tmp,sizeof(osc_m_tmp)) == 0 )
	{
		/* check */
		if( memcmp(&tdp,&osc_m_tmp,sizeof(tdp)) == 0 )
		{
			return FS_OK;
		}
	}
	/* cal check sum */
	runmsg->check = osc_param_check((unsigned char *)runmsg,sizeof(osc_m_tmp)- 1);
	/* copy data */
	memcpy(&osc_m_tmp,runmsg,sizeof(osc_m_tmp));		
	/* save data */
	osc_k24c02_write(90+2,&osc_m_tmp,sizeof(osc_m_tmp));	
	/* read */
	osc_k24C02_read(90+2,&tdp,sizeof(tdp));		
	/* check */
	if( memcmp(&tdp,&osc_m_tmp,sizeof(tdp)) == 0 )
	{
		return FS_OK;
	}
	else
	{
		return FS_ERR;
	}
}
/* thread */
void osc_param_thread(void)
{
	/* get sta */
	iic_sta = hal_iic_busy();
	/* save */
	if( iic_sta < 20 )//2s
	{
		/* return */
		return;
	}
	/* tesf */
	unsigned char * tmp = (unsigned char *)&osc_m_tmp;
	unsigned char * rum = (unsigned char *)runmsg;
	/* check */
	for( int i = 0 ; i < sizeof(osc_m_tmp) ; i ++ )
	{
		/* check */
		if( tmp[i] != rum[i] )
		{
			/* get sem */
			if( hal_iic_sem_get() == 1 )
			{
				break;
			}
			/* set sem */
			hal_iic_sem_set(1);
			/* save to eeprom */
			osc_k24c02_write(90+2+i,&rum[i],1);
			/* update */
			tmp[i] = rum[i];
			/* set check */
			runmsg->check = osc_param_check((unsigned char *)runmsg,sizeof(osc_m_tmp)- 1);
			/* release sem */
			hal_iic_sem_set(0);			
			/* break */
			break;
		}
	}
}























